#!/usr/bin/python
# -*- coding: UTF-8 -*-
#cron:0 0,7,12 * * *
import datetime
from lunarcalendar import Converter, Lunar, Solar
import wxpusher
from datetime import date
from dateutil.relativedelta import relativedelta

def get_days_until_lunar_birthday(lunar_month, lunar_day):
    today = datetime.date.today()
    lunar_today = Converter.Solar2Lunar(today)

    # 创建一个Lunar对象表示今天
    lunar_date = Lunar(lunar_today.year, lunar_today.month, lunar_today.day)

    # 创建一个Lunar对象表示生日
    birthday = Lunar(lunar_today.year, lunar_month, lunar_day)

    # 将生日转换为公历日期
    solar_birthday = Converter.Lunar2Solar(birthday).to_date()

    # 如果今年的生日已经过了，计算明年的生日
    if solar_birthday < today:
        birthday = Lunar(lunar_today.year + 1, lunar_month, lunar_day)
        solar_birthday = Converter.Lunar2Solar(birthday).to_date()

    days_until_birthday = (solar_birthday - today).days
    return days_until_birthday

def generate_html(birthdays):
    html_content = """
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>生日提醒</title>
        <style>
            body {
                font-family: 'Microsoft YaHei', Arial, sans-serif;
                background-color: #f0f4f8;
                color: #333;
                margin: 0;
                padding: 20px;
                line-height: 1.6;
            }
            .container {
                max-width: 800px;
                margin: 30px auto;
                padding: 20px;
                background-color: #fff;
                border-radius: 8px;
                box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            }
            h1 {
                text-align: center;
                color: #333;
                margin-bottom: 20px;
            }
            .birthday-card {
                border-left: 4px solid #007bff;
                padding: 15px;
                margin-bottom: 10px;
                background-color: #fff;
                box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            }
            .birthday-name {
                font-size: 20px;
                color: #007bff; /* Changed to blue */
                margin-bottom: 5px;
            }
            .birthday-days {
                font-size: 18px;
                margin-bottom: 5px;
            }
            .birthday-date {
                font-size: 16px;
                color: #666;
            }
            @media (max-width: 768px) {
                body {
                    padding: 10px;
                }
                .container {
                    margin: 10px auto;
                }
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>生日提醒</h1>
    """
    for person, days, new_date, age in sorted(birthdays, key=lambda x: x[1]):
        if days == 0:
            days_message = f"今天是{age}周岁生日！🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂"
        else:
            days_message = f"距离{age+1}周岁生日还有：{days}天"
        html_content += f"""
            <div class="birthday-card">
                <div class="birthday-name">{person}</div>
                <div class="birthday-days">{days_message}</div>
                <div class="birthday-date">公历生日日期：{new_date.year}年{new_date.month}月{new_date.day}日</div>
            </div>
        """
    html_content += """
        </div>
    </body>
    </html>
    """
    return html_content

def main():
    people_birthdays = {
        "妹妹": (10, 28, 2000),
        "星辰宝宝": (1, 5, 2024),
        "燕婷": (3, 11, 1995),
        "老爸": (8, 18, 1971),
        "老妈": (12, 6, 1971),
        "我": (9, 23, 1994),
    }

    # 计算每个人距离生日的天数，并存储在列表中
    days_until_birthdays = []
    current_date = date.today()
    for person, (lunar_month, lunar_day,chushengyear) in people_birthdays.items():
        days = get_days_until_lunar_birthday(lunar_month, lunar_day)
        delta = relativedelta(days=days)
        # 将当前日期加上天数，得到新日期
        new_date = current_date + delta
        age = int(datetime.date.today().year) - chushengyear
        days_until_birthdays.append((person, days, new_date,age))
    
    # 生成HTML内容
    html_content = generate_html(days_until_birthdays)
    return html_content

if __name__ == "__main__":
    title = '生日提醒'
    contents = main()
    wxpusher.send(title, contents)
